package main

import (
	"errors"
	"fmt"
)

//递归函数 斐波拉契数列

func fib(n int) (res int) {
	if n <= 2 {
		return 1
	}
	return fib(n-1) + fib(n-2)
}

func fib2(x int) (res int, err error) {
	if x > 0 && x <= 2 {
		return 1, nil
	} else if x < 0 {
		err = errors.New("x must be greater than 0 and less than or equal to 2")
		return
	} else {
		res = fib(x-1) + fib(x-2)
		return
	}
}

// 斐波拉契数列
func Fibonacci(position int) (int, int) {
	if position <= 0 {
		return 0, 0
	} else if position == 1 {
		return 1, 1
	}

	prev1, _ := Fibonacci(position - 1)
	prev2, _ := Fibonacci(position - 2)

	return prev1 + prev2, position
}

//打印10到1数字递归

func fib4(n int) {
	if n > 0 {
		fmt.Printf("%d", n)
		fib4(n - 1)
	}
}

// 阶乘
func factorial(n int) int {
	if n == 0 {
		return 1
	}
	return n * factorial(n-1)
}

func main() {
	result := 0
	for i := 1; i <= 10; i++ {
		result = fib(i)
		fmt.Println(result)
	}
	for i := -1; i <= 10; i++ {
		// result, err := fib2(i)
		// fmt.Println(result, err)
		position, value := Fibonacci(i)
		fmt.Printf("Position: %d, Value: %d\n", position, value)
	}
	fib4(10)
	var s int
	s = factorial(10)
	fmt.Println(s)
}
